﻿<?php

require_once ('ConnectDB.php');
require_once ('SuKienDTO.php');

class SuKienDAO extends ConnectDB {
	function SuKienDAO(){
		
	}
	
	/**
	 * Thêm sự kiện
	 * Enter description here ...
	 */
	public static function ThemSuKien($suKienDto)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "INSERT INTO su_kien (MaSuKien, MaGianHang, TenSuKien, HinhAnh, NoiDungSuKien,NgayTao, NgayBatDau, NgayKetThuc, NgayCapNhat, NguoiCapNhat, NgayXoa, NguoiXoa,DaXoa,PhanTramGiamGia,QuaTang)
								VALUES('$suKienDto->MaSuKien',
										'$suKienDto->MaGianHang',
										N'$suKienDto->TenSuKien',
										'$suKienDto->HinhAnh',
										N'$suKienDto->NoiDungSuKien',
										'$suKienDto->NgayTao',
										'$suKienDto->NgayBatDau',
										'$suKienDto->NgayKetThuc',
										NULL, NULL, NULL, NULL,
										'$suKienDto->DaXoa',
										$suKienDto->PhanTramGiamGia,
										N'$suKienDto->QuaTang');";
			//echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	
/**
	 * Cập nhật sự kiện
	 * Edit by Thu Hà 25/6/2011	 
	 */
	public static function CapNhatThongTinSuKien($suKienDto)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "UPDATE su_kien 
						SET TenSuKien = N'$suKienDto->TenSuKien', 
							HinhAnh = '$suKienDto->HinhAnh',
							NgayBatDau = '$suKienDto->NgayBatDau', 
							NgayKetThuc= '$suKienDto->NgayKetThuc', 
							NgayCapNhat = '$suKienDto->NgayCapNhat', 
							NguoiCapNhat = $suKienDto->NguoiCapNhat,
							PhanTramGiamGia = $suKienDto->PhanTramGiamGia, 
							QuaTang = '$suKienDto->QuaTang'
						WHERE MaSuKien = $suKienDto->MaSuKien AND DaXoa = 0;";
			
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
			$result = FALSE;
		}
		return $result;
	}
	
	/**
	 * Xóa xự kiện
	 * Edit by Thu Hà 20/6/2011	 
	 */
	public static function XoaSuKien($maSuKien, $maNguoiDung)
	{
		$result = true;
		try {
			if (!ConnectDB::OpenConnection())
			return FALSE;
			
			$strSQL = "UPDATE su_kien as sk 
						SET sk.DaXoa = 1, sk.NgayXoa = now() , sk.NguoiXoa = $maNguoiDung 
						WHERE sk.MaSuKien = $maSuKien;";
			echo $strSQL;
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			//echo $strSQL;
			ConnectDB::CloseConnection();
			
		} catch (Exception $e) {
				$result = FALSE;
		}
		return $result;
	}
	
	/**
	 * Lấy ds sự kiện
	 * $tinhTrang = 1: đã xóa, 0 chưa xóa, 2 tất cả
	 * Edit by Thu Hà 20/6/2011	  
	 */
	public  static  function LayDanhSachSuKien($tinhTrang)
	{
		$lstSuKien = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "select * from Su_Kien";
			if ($tinhTrang != 2)
			{
				$strSQL .= " WHERE DaXoa = $tinhTrang";
			}
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if(!$result)
				$lstSuKien = null;
				
			while ($row = mysql_fetch_array($result) )
			{
				$suKienDto = new SuKienDTO();
				$suKienDto->MaSuKien = $row["MaSuKien"];
				$suKienDto->MaGianHang = $row["MaGianHang"];
				$suKienDto->TenSuKien = $row["TenSuKien"];
				$suKienDto->HinhAnh = $row["HinhAnh"];
				$suKienDto->NoiDungSuKien = $row["NoiDungSuKien"];
				$suKienDto->NgayTao = $row["NgayTao"];
				$suKienDto->NgayBatDau = $row["NgayBatDau"];
				$suKienDto->NgayKetThuc = $row["NgayKetThuc"];
				$suKienDto->NgayCapNhat = $row["NgayCapNhat"];
				$suKienDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$suKienDto->NgayXoa = $row["NgayXoa"];
				$suKienDto->NguoiXoa = $row["NguoiXoa"];
				$suKienDto->DaXoa = $row["DaXoa"];
				$suKienDto->PhanTramGiamGia = $row["PhanTramGiamGia"];
				$suKienDto->QuaTang = $row["QuaTang"];
				
				array_push($lstSuKien, $suKienDto);		
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSuKien = null;
		}
		return $lstSuKien;
	}
	
	/**
	 * Lay su kien theo ma su kien
	 */
	public  static  function LaySuKienTheoMaSuKien($maSuKien)
	{
		$suKienDto = new SuKienDTO();
		try {
			if (!ConnectDB::OpenConnection())
				return;
				
			$strSQL = "select * from Su_Kien sk where sk.MaSuKien = $maSuKien AND sk.DaXoa = 0;";
			//echo $strSQL;
			
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if(!$result)
			{
				return null;
			}
			if (mysql_num_rows($result) <= 0)
			{
				return null;
			}
			while ($row = mysql_fetch_array($result) )
			{
				
				$suKienDto->MaSuKien = $row["MaSuKien"];
				$suKienDto->MaGianHang = $row["MaGianHang"];
				$suKienDto->TenSuKien = $row["TenSuKien"];
				$suKienDto->HinhAnh = $row["HinhAnh"];
				$suKienDto->NoiDungSuKien = $row["NoiDungSuKien"];
				$suKienDto->NgayTao = $row["NgayTao"];
				$suKienDto->NgayBatDau = $row["NgayBatDau"];
				$suKienDto->NgayKetThuc = $row["NgayKetThuc"];
				$suKienDto->NgayCapNhat = $row["NgayCapNhat"];
				$suKienDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$suKienDto->NgayXoa = $row["NgayXoa"];
				$suKienDto->NguoiXoa = $row["NguoiXoa"];
				$suKienDto->DaXoa = $row["DaXoa"];				
				$suKienDto->PhanTramGiamGia = $row["PhanTramGiamGia"];
				$suKienDto->QuaTang = $row["QuaTang"];
					
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$suKienDto = null;
		}
		return $suKienDto;
	}
	
	/**
	 * Lấy danh sách sự kiện theo gian hàng
	 * @param unknown_type $nguoidungDto
	 */
	public  static  function LayDanhSachSuKienTheoGianHang($maGianHang)
	{
		$lstSuKien = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "	select * from Su_Kien sk 
					   	where sk.MaGianHang = $maGianHang  AND sk.DaXoa = 0
						ORDER BY sk.NgayBatDau DESC;";
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			if(!$result)
				return null;
			while ($row = mysql_fetch_array($result) )
			{
				$suKienDto = new SuKienDTO();
				$suKienDto->MaSuKien = $row["MaSuKien"];
				$suKienDto->MaGianHang = $row["MaGianHang"];
				$suKienDto->TenSuKien = $row["TenSuKien"];
				$suKienDto->HinhAnh = $row["HinhAnh"];
				$suKienDto->NoiDungSuKien = $row["NoiDungSuKien"];
				$suKienDto->NgayTao = $row["NgayTao"];
				$suKienDto->NgayBatDau = $row["NgayBatDau"];
				$suKienDto->NgayKetThuc = $row["NgayKetThuc"];
				$suKienDto->NgayCapNhat = $row["NgayCapNhat"];
				$suKienDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$suKienDto->NgayXoa = $row["NgayXoa"];
				$suKienDto->NguoiXoa = $row["NguoiXoa"];
				$suKienDto->DaXoa = $row["DaXoa"];
				$suKienDto->PhanTramGiamGia = $row["PhanTramGiamGia"];
				$suKienDto->QuaTang = $row["QuaTang"];
								
				array_push($lstSuKien, $suKienDto);		
			}
			ConnectDB::CloseConnection();				
			
		} catch (Exception $e) {
			$lstSuKien = null;
		}
		return $lstSuKien;
	}
	
	/**
	 * Lấy ds sự kiện theo tên sự kiện
	 */
	public  static  function LayDanhSachSuKienTheoTenSuKien($tenSuKien)
	{
		$lstSuKien = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "select * from Su_Kien sk where sk.TenSuKien like $tenSuKien;";
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			if(!$result)
				return null;
				
			while ($row = mysql_fetch_array($result) )
			{
				$suKienDto = new SuKienDTO();
				$suKienDto->MaSuKien = $row["MaSuKien"];
				$suKienDto->MaGianHang = $row["MaGianHang"];
				$suKienDto->TenSuKien = $row["TenSuKien"];
				$suKienDto->HinhAnh = $row["HinhAnh"];
				$suKienDto->NoiDungSuKien = $row["NoiDungSuKien"];
				$suKienDto->NgayTao = $row["NgayTao"];
				$suKienDto->NgayBatDau = $row["NgayBatDau"];
				$suKienDto->NgayKetThuc = $row["NgayKetThuc"];
				$suKienDto->NgayCapNhat = $row["NgayCapNhat"];
				$suKienDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$suKienDto->NgayXoa = $row["NgayXoa"];
				$suKienDto->NguoiXoa = $row["NguoiXoa"];
				$suKienDto->DaXoa = $row["DaXoa"];
				$suKienDto->PhanTramGiamGia = $row["PhanTramGiamGia"];
				$suKienDto->QuaTang = $row["QuaTang"];
				
				array_push($lstSuKien, $sanPhamDto);		
			}
			ConnectDB::CloseConnection();				
			
		} 
		catch (Exception $e) {
			$lstSuKien = null;
		}
		return $lstSuKien;
	}
	
	/**
	 * Lấy ds sự kiện trong khoảng thời gian
	 * Edit by Thu Hà 20/6/2011	 
	 */
	public  static  function LayDanhSachSuKienTrongKhoangThoiGian($maGH, $thoiGianBD, $thoiGianKT)
	{
		$lstSuKien = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "SELECT * FROM su_kien 
						WHERE MaGianHang = $maGH AND su_kien.DaXoa = 0 ";
			if (strlen($thoiGianBD) > 0)
				$strSQL .= " AND su_kien.NgayBatDau >= $thoiGianBD "; 
			if (strlen($thoiGianKT) > 0)
				$strSQL .= " AND su_kien.NgayKetThuc <= $thoiGianKT";
			echo $strSQL;
								
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			if(!$result)
				return null;
				
			while ($row = mysql_fetch_array($result) )
			{
				$suKienDto = new SuKienDTO();
				$suKienDto->MaSuKien = $row["MaSuKien"];
				$suKienDto->MaGianHang = $row["MaGianHang"];
				$suKienDto->TenSuKien = $row["TenSuKien"];
				$suKienDto->HinhAnh = $row["HinhAnh"];
				$suKienDto->NoiDungSuKien = $row["NoiDungSuKien"];
				$suKienDto->NgayTao = $row["NgayTao"];
				$suKienDto->NgayBatDau = $row["NgayBatDau"];
				$suKienDto->NgayKetThuc = $row["NgayKetThuc"];
				$suKienDto->NgayCapNhat = $row["NgayCapNhat"];
				$suKienDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$suKienDto->NgayXoa = $row["NgayXoa"];
				$suKienDto->NguoiXoa = $row["NguoiXoa"];
				$suKienDto->DaXoa = $row["DaXoa"];
				$suKienDto->PhanTramGiamGia = $row["PhanTramGiamGia"];
				$suKienDto->QuaTang = $row["QuaTang"];
								
				array_push($lstSuKien, $sanPhamDto);		
			}
			ConnectDB::CloseConnection();				
			
		} 
		catch (Exception $e) {
			$lstSuKien = null;
		}
		return $lstSuKien;
	}
	/**
	 * Lấy ds sự kiện nổi bật của gian hàng
	 * Edit by Ngọc Hà 21/6/2011	
	 * edit 25/6/2011 Thu Hà 
	 */
	public  static  function LaySuKienNoiBatCuaGianHang($maGianHang)
	{
		$suKienDto = new SuKienDTO();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "SELECT * FROM su_kien 
						WHERE su_kien.DaXoa = 0 AND su_kien.MaGianHang = $maGianHang
						ORDER BY su_kien.NgayBatDau DESC limit 0,1;";
			
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			if(!$result || mysql_num_rows($result) != 1)
				return null;
				
			$row = mysql_fetch_array($result);
							
			$suKienDto->MaSuKien = $row["MaSuKien"];
			$suKienDto->MaGianHang = $row["MaGianHang"];
			$suKienDto->TenSuKien = $row["TenSuKien"];
			$suKienDto->HinhAnh = $row["HinhAnh"];
			$suKienDto->NoiDungSuKien = $row["NoiDungSuKien"];
			$suKienDto->NgayTao = $row["NgayTao"];
			$suKienDto->NgayBatDau = $row["NgayBatDau"];
			$suKienDto->NgayKetThuc = $row["NgayKetThuc"];
			$suKienDto->NgayCapNhat = $row["NgayCapNhat"];
			$suKienDto->NguoiCapNhat = $row["NguoiCapNhat"];
			$suKienDto->NgayXoa = $row["NgayXoa"];
			$suKienDto->NguoiXoa = $row["NguoiXoa"];
			$suKienDto->DaXoa = $row["DaXoa"];
			$suKienDto->PhanTramGiamGia = $row["PhanTramGiamGia"];
			$suKienDto->QuaTang = $row["QuaTang"];
								
			
			ConnectDB::CloseConnection();				
			
		} 
		catch (Exception $e) {
			$suKienDto = null;
		}
		return $suKienDto;
	}
	
	/**
	 * Lấy ds sự kiện sắp diễn ra
	 * Anh Vu 21/6/2011	 
	 */
	public  static  function LayDanhSachSuKienSapDienRa()
	{
		$lstSuKien = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "SELECT * FROM su_kien 
						WHERE DATEDIFF(now(),su_kien.NgayBatDau) < 0 
						AND su_kien.DaXoa = 0
						ORDER BY su_kien.NgayBatDau DESC LIMIT 0,3";
						
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			if(!$result)
				return null;
				
			while ($row = mysql_fetch_array($result) )
			{
				$suKienDto = new SuKienDTO();
				$suKienDto->MaSuKien = $row["MaSuKien"];
				$suKienDto->MaGianHang = $row["MaGianHang"];
				$suKienDto->TenSuKien = $row["TenSuKien"];
				$suKienDto->HinhAnh = $row["HinhAnh"];
				$suKienDto->NoiDungSuKien = $row["NoiDungSuKien"];
				$suKienDto->NgayTao = $row["NgayTao"];
				$suKienDto->NgayBatDau = $row["NgayBatDau"];
				$suKienDto->NgayKetThuc = $row["NgayKetThuc"];
				$suKienDto->NgayCapNhat = $row["NgayCapNhat"];
				$suKienDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$suKienDto->NgayXoa = $row["NgayXoa"];
				$suKienDto->NguoiXoa = $row["NguoiXoa"];
				$suKienDto->DaXoa = $row["DaXoa"];
								
				array_push($lstSuKien, $suKienDto);		
			}
			ConnectDB::CloseConnection();				
			
		} 
		catch (Exception $e) {
			$lstSuKien = null;
		}
		return $lstSuKien;
	}
	
	
	/**
	 * Lấy ds sự kiện đang diễn ra
	 * Anh Vu 21/6/2011	 
	 */
	public  static  function LayDanhSachSuKienDangDienRa()
	{
		$lstSuKien = array();
		try {
			if (!ConnectDB::OpenConnection())
				return false;
				
			$strSQL = "SELECT * FROM su_kien 
						WHERE DATEDIFF(now(),su_kien.NgayBatDau) >= 0 
						AND su_kien.DaXoa = 0
						ORDER BY su_kien.NgayBatDau DESC LIMIT 0,3";
						
			$result = mysql_query($strSQL, ConnectDB::$mLink);
			
			if(!$result)
				return null;
				
			while ($row = mysql_fetch_array($result) )
			{
				$suKienDto = new SuKienDTO();
				$suKienDto->MaSuKien = $row["MaSuKien"];
				$suKienDto->MaGianHang = $row["MaGianHang"];
				$suKienDto->TenSuKien = $row["TenSuKien"];
				$suKienDto->HinhAnh = $row["HinhAnh"];
				$suKienDto->NoiDungSuKien = $row["NoiDungSuKien"];
				$suKienDto->NgayTao = $row["NgayTao"];
				$suKienDto->NgayBatDau = $row["NgayBatDau"];
				$suKienDto->NgayKetThuc = $row["NgayKetThuc"];
				$suKienDto->NgayCapNhat = $row["NgayCapNhat"];
				$suKienDto->NguoiCapNhat = $row["NguoiCapNhat"];
				$suKienDto->NgayXoa = $row["NgayXoa"];
				$suKienDto->NguoiXoa = $row["NguoiXoa"];
				$suKienDto->DaXoa = $row["DaXoa"];
								
				array_push($lstSuKien, $suKienDto);		
			}
			ConnectDB::CloseConnection();				
			
		} 
		catch (Exception $e) {
			$lstSuKien = null;
		}
		return $lstSuKien;
	}
}

?>